package com.lkedu.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lkedu.entity.UserBean;
import com.lkedu.service.AdminService;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	AdminService adminService= new AdminService();
	//获取所有用户
	public void getUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------getUsers----------");
		//调用业务，获取用户集合
		
		List<UserBean> userBeans=adminService.getUsers();
		
		//将查询的结果放在requestScope
		request.setAttribute("userBeans", userBeans);
		request.getRequestDispatcher("admin.jsp").forward(request, response);
	}
	
	//禁用用户-修改state
	public void disState(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------disState----------");
		String uid=request.getParameter("uid");
		int result=adminService.disState(uid);
		response.sendRedirect("AdminServlet?method=getUsers");
	}
	
	public void delUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------delUser----------");
		String uid=request.getParameter("uid");
		int result=adminService.delUser(uid);
		response.sendRedirect("AdminServlet?method=getUsers");
	}
	public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------save----------");
		String uid=request.getParameter("uid");
		String username=request.getParameter("username");
		String phone=request.getParameter("phone");
		String email=request.getParameter("email");
		String sex=request.getParameter("sex");
		int result=adminService.save(uid,username,phone,email,sex);
		response.sendRedirect("AdminServlet?method=getUsers");
	}
	
}
